package com.aode.downloadmusic.repository;

import android.app.Application;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.aode.common.bean.response.Music;
import com.aode.utils.bitmap.BitmapUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Repository {

    private MutableLiveData<List<Music>> musicMutableLiveData = new MutableLiveData<>();
    private List<Music> musicList = new ArrayList<>();
    private Application application;
    public Repository(Application application){
        this.application = application;
    }
    public void loadLocalMusicData() {
        /**
         * 加载本地存储当中的音乐mp3文件到集合当中
         */
        //1.获取ContentResolver对象
        ContentResolver resolver = application.getContentResolver();
        //2.获取本地音乐存储的Uri地址
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        //3.开始查询地址
        Cursor cursor = resolver.query(uri, null, null, null, null);
        //4.遍历Cursor
        int id = 0;
//        Log.e("sddsd",cursor.moveToNext()+"");
        while(cursor.moveToNext()){
            String song = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            String singer = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
            id++;
            String sid = String.valueOf(id);
            String index = sid;
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
            SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
            String time = sdf.format(new Date(duration));
            //获取专辑图片主要是通过album_id进行查询
            String album_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
            String albumArt = getAlbumArt(album_id);
            Log.e("TAG",time);

            //将一行当中的数据封装到对象当中
            Music music = new Music();
            music.setMusicName(song);
            music.setMusicMusician(singer);
            music.setMusicProduct(path);
            music.setMusicCover(albumArt);
            music.setMusicCover(albumArt);

            musicList.add(music);
        }
        musicMutableLiveData.setValue(musicList);
    }
    public MutableLiveData<List<Music>> getMusicMutableLiveData(){
        return musicMutableLiveData;
    }


    private String getAlbumArt(String album_id) {
        String mUriAlbums = "content://media/external/audio/albums";
        String[] projection = new String[]{"album_art"};
        Cursor cur =application.getContentResolver().query(
                Uri.parse(mUriAlbums + "/" + album_id),
                projection, null, null, null);
        String album_art = null;
        if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
            cur.moveToNext();
            album_art = cur.getString(0);
        }
        cur.close();
        cur = null;
        return album_art;
    }

}
